<?php
/**
 * This is the template for generating CRUD search class of the specified model.
 */

use yii\helpers\StringHelper;
use yii\helpers\Inflector;


/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\crud\Generator */

$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
if ($modelClass === $searchModelClass) {
    $modelAlias = $modelClass . 'Model';
}
$rules = $generator->generateSearchRules();
$labels = $generator->generateSearchLabels();
$searchAttributes = $generator->getSearchAttributes();
$searchConditions = $generator->generateSearchConditions();

echo "<?php\n";
?>

namespace <?= StringHelper::dirname(ltrim($generator->searchModelClass, '\\')) ?>;

use app\models\searchs\ClientFilterInterface;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use <?= ltrim($generator->modelClass, '\\') . (isset($modelAlias) ? " as $modelAlias" : "") ?>;
use yii\data\DataFilter;

/**
 * <?= $searchModelClass ?> represents the model behind the search form of `<?= $generator->modelClass ?>`.
 */
class <?= $searchModelClass ?> extends <?= isset($modelAlias) ? $modelAlias : $modelClass ?> implements ClientFilterInterface

{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            <?= implode(",\n            ", $rules) ?>,
        ];
    }

    public function filters()
    {
        return [
<?php
if (($tableSchema = $generator->getTableSchema()) === false) {
    foreach ($generator->getColumnNames() as $name) {
        echo "            '" . $name . "' => ['like' => ''],\n";
    }
} else {
    foreach ($tableSchema->columns as $column) {
        switch($column->phpType){
            case 'string':
                echo "            '" . $column->name . "' => ['like' => ''],\n";
                break;
            case 'boolean':
                echo "            '" . $column->name . "' => ['eq' => 0],\n";
                break;
            case 'integer':
                echo "            '" . $column->name . "' => ['eq' => 0],\n";
                break;
            case 'double':
                echo "            '" . $column->name . "' => ['eq' => 0],\n";
                break;
            case 'array':
                break;
            default:
                break;
        }
    }
}
?>
        ];
    }

    public function attributeLabels()
    {
        return [
<?php
    foreach ($searchAttributes as $label){
        echo "            '{$label}' => " . $generator->generateString(Inflector::camel2words($label)) . ",\n";
    }
?>
        ];
    }

    public function attributeTypes()
    {
        return [
<?php
if (($tableSchema = $generator->getTableSchema()) === false) {
    foreach ($generator->getColumnNames() as $name) {
        echo "            '" . $name . "' => DataFilter::TYPE_STRING,\n";
    }
} else {
    foreach ($tableSchema->columns as $column) {
        $format = $generator->generateColumnFormat($column);
        switch($column->phpType){
            case 'string':
                echo "            '" . $column->name . "' => DataFilter::TYPE_STRING,\n";
                break;
            case 'boolean':
                echo "            '" . $column->name . "' => DataFilter::TYPE_BOOLEAN,\n";
                break;
            case 'integer':
                echo "            '" . $column->name . "' => DataFilter::TYPE_INTEGER,\n";
                break;
            case 'double':
                echo "            '" . $column->name . "' => DataFilter::TYPE_FLOAT,\n";
                break;
            case 'array':
                echo "            '" . $column->name . "' => DataFilter::TYPE_ARRAY,\n";
                break;
            default:
                break;
        }
    }
}
?>
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = <?= isset($modelAlias) ? $modelAlias : $modelClass ?>::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,<?= (in_array('id', $searchAttributes)) ? "\n            'sort' => ['defaultOrder' => ['id' => SORT_DESC]]\n" : '' ?>
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        <?= implode("\n        ", $searchConditions) ?>

        return $dataProvider;
    }
}
